<template>
    <div class="goodinfo">
        <transition 
        @before-enter="beforeEnter"
        @enter="enter"
        @after-enter="afterEnter" >
        <!-- 这是那个小球的元素 -->
            <div class="ball" v-show="showball" ref="ball"></div>
        </transition>
        
        <!-- 商品轮播图区域 -->
        <div class="mui-card">
				<div class="mui-card-content">
					<div class="mui-card-content-inner">
						<swiper class="phone-swiper" :lunbolist="lunbolist" :iswidthfull="false" :isheightfull="true"></swiper>
					</div>
				</div>
			</div>
        <!--商品购买区域-->
        <div class="mui-card">
				<div class="mui-card-header">{{goodinfo.title}}</div>
				<div class="mui-card-content">
					<div class="mui-card-content-inner">
						市场价：<span class="market-price">￥{{goodinfo.market_price}}</span>&nbsp;&nbsp;&nbsp;
                        销售价：<span class="sell-price">￥{{goodinfo.sell_price}}</span>
                        <p class="buy-number">
                            <span class="but-number1">购买数量：</span>
                            <!-- 这里是重点 -->
                            <numberbox @getcount="getselectcount" :max="goodinfo.stock_quantity"></numberbox>
                        </p>
                        <div>
                            <mt-button type="primary" size="small">立即购买</mt-button>
                            <mt-button type="danger" size="small" @click="isshowball">加入购物车</mt-button>
                        </div>
					</div>
				</div>
				
			</div>
        <!-- 商品参数区域 -->
        <div class="mui-card">
				<div class="mui-card-header">商品参数</div>
				<div class="mui-card-content">
					<div class="mui-card-content-inner">
						<p>商品货号：{{goodinfo.good_no}}</p>
						<p>库存情况：{{goodinfo.stock_quantity}}件</p>
						<p>上架时间：{{goodinfo.add_time}}</p>
					</div>
				</div>
				<div class="mui-card-footer bottom-button">
                    <mt-button @click="imgtext(id)" type="primary" size="large">图文介绍</mt-button>
                    <mt-button @click="shopcomment(id)" type="danger" size="large">商品评论</mt-button>
                </div>
		</div>
    </div>
</template>


<script>
/**
 * 该页面解决了两个问题
 * 1.如何在不同分辨率和不同的页面大小下小球都可以正确的移动到购物车。
 * 2.如何实现当小球加入到购物车之后，页面内选择数量正确改变。
 * ---本质是子组件向父组件传值（事件调用机制）。
 */
import swiper from '../../newsinfosubcomp/swiper.vue'
import numberbox from '../../newsinfosubcomp/numberbox.vue'
export default {
    data(){
        return{
            id:this.$route.params.id,
            lunbolist:[
                {id:1,src:"https://img.alicdn.com/bao/uploaded/i3/TB1fOtnbDZmx1VjSZFGj3Sx2XXa_025424.jpg"},
                {id:2,src:"https://img.alicdn.com/bao/uploaded/i5/TB1OCArhZfpK1RjSZFORAi6nFXa_025954.jpg"},
                
            ],
            goodinfo:{add_time:"2015-1-1 12:11:12",good_no:"11122323232",market_price:1212,sell_price:1000,stock_quantity:20,title:"华为荣耀10 在线销售"},
            showball:false,    
            selectcount:1
            
        }
    },
    created(){
        this.getlunbotu()
        this.getgoodinfo()
    },
    methods:{
        getlunbotu(){
            this.$http.get("api/images/"+this.id).then(result=>{
                if(result.body.status===0){
                    this.lunbolist=result.body.message.forEach(element => {
                        element.img=element.src
                    });
                }
            })
        },
        getgoodinfo(){
            this.$http.get("api/goodinfo"+this.id).then(result=>{
                if(result.body.status===0){
                    this.goodinfo=result.body.message[0]
                }
            })
        },
        imgtext(id){
            this.$router.push({name:"imgtext",params:{id}})
        },
        shopcomment(id){
            this.$router.push({name:"shopcomment",params:{id}})
        },
        isshowball(){
            // 第一次点击显示小球
            this.showball=!this.showball
            // 拼接一个加入购物车的商品的对象。
            var goodinfo={
                id:this.id,
                count:this.selectcount,
                price:this.goodinfo.sell_price,
                selected:true
            }
            // 这是同步到main.js中的store的mutations的addtocar方法中去了。
            this.$store.commit('addtocar',goodinfo)
        },
        beforeEnter(el){
            // 设置小球进入之前的位置
            el.style.transform="translate(0,0)"
        },
        enter(el,done){
            //这个是获取本页面内的元素的位置
            const ballposition=this.$refs.ball.getBoundingClientRect();
            // 该方法可以获取不是本页面内的元素的位置
            const badgeposition=document.getElementById('badge').getBoundingClientRect();
            const xpos=badgeposition.left-ballposition.left
            const ypos=badgeposition.top-ballposition.top
            el.offsetWidth;
            // 设置移动到的位置,这是用了es6的模板字符串的方法。
            el.style.transform=`translate(${xpos}px,${ypos}px)`
            //设置移动过程中的动画
            el.style.transition="all 1s ease"
            done()

        },
        afterEnter(el){
            // 隐藏小球
            this.showball=!this.showball
        },
        getselectcount(count){
            this.selectcount=count;
            // console.log("父组件得到的值:"+count)
        }

    },
    components:{
        swiper,
        numberbox
    }
}
</script>

<style lang="scss" scoped>
    .goodinfo{
        background-color: #eee;
        .phone-swiper{
            text-align: center;
        }
        
    }
    .mui-card{
        .market-price{
            text-decoration: line-through;
        }
        .sell-price{
            font-weight: bold;
            color: red;
        }
        .buy-number{
            
            display: flex;
            padding: 10px 0px 5px 0px;
            .but-number1{
                display: flex;
                align-items:center;  
                color: #111; 
            }
           
        }
        
    }
    .bottom-button{
        display: block;
        :first-child{
            margin: 0px 0px 5px 0px;
        }
    }
    .ball{
        height: 20px;
        width: 20px;
        background-color: red;
        border-radius: 50%;
        position: absolute;
        z-index: 999;
        left: 144px;
        top: 386px;
        
    }
</style>

